/**
 * @file usr_pd_i2c.c
 * @author 黑马小乌龟 (532352406@qq.com)
 * @brief 
 * @version 0.1
 * @date 2025-10-11
 * 
 * @copyright Copyright (c) 2025
 * 
 */
#include "usr_cfg.h"
#include "usr_uart.h"
#include "usr_pd_i2c.h"


int32_t usr_PD_I2C_Write(uint16_t u16Addr, const uint8_t *pu8Buf, uint32_t u32Len) {
    uint16_t u16MemAddrTemp;

    u16MemAddrTemp = u16Addr;

    return BSP_I2C_Write(BSP_24CXX_I2C_UNIT, CFG_PD_I2C_SLAVE_ADDR, (const uint8_t *)&u16MemAddrTemp, EE_24CXX_MEM_ADDR_LEN, pu8Buf, u32Len);
}

int32_t usr_PD_I2C_Read(uint16_t u16Addr, uint8_t *pu8Buf, uint32_t u32Len) {
    uint16_t u16MemAddrTemp;

    u16MemAddrTemp = u16Addr;

    return BSP_I2C_Read(BSP_24CXX_I2C_UNIT, CFG_PD_I2C_SLAVE_ADDR, (const uint8_t *)&u16MemAddrTemp, EE_24CXX_MEM_ADDR_LEN, pu8Buf, u32Len);
}
void usb_pd_int_gpio_init(void){
    stc_gpio_init_t stcGpioInit;

    (void)GPIO_StructInit(&stcGpioInit);
    // stcGpioInit.u16PinState = PIN_STAT_RST;
    stcGpioInit.u16PinDir = PIN_DIR_IN;
    stcGpioInit.u16PullUp = PIN_PU_ON;
    (void)GPIO_Init(PD_CHIP_INT_PORT, PD_CHIP_INT_PIN, &stcGpioInit);
}

void usb_pd_information_handle(uint8_t sync_flg) {
    int ret;
    
    if (GPIO_ReadInputPins(PD_CHIP_INT_PORT, PD_CHIP_INT_PIN) == PIN_RESET || sync_flg) {
        uint8_t pd_run_time_infor;
        ret = usr_PD_I2C_Read(1, &pd_run_time_infor, 1);
        uint8_t dc_in_flg, usb_in_flg;
        USB_INPUT_INFOR usb_infor;
        char *p_dc_in_str = "", *p_usb_in_str = "", *dc_or_usb = "";
        char str_buf[64];

        if (LL_OK == ret) {
            DDL_Printf("pd-infor:%x\n", pd_run_time_infor);
            if (main_dat.pd_state != pd_run_time_infor || sync_flg) {
                main_dat.pd_state = pd_run_time_infor;
                dc_in_flg = pd_run_time_infor & DC_IN_FLG;
                usb_in_flg = pd_run_time_infor & USB_IN_FLG;
                usb_infor = pd_run_time_infor & 0x07;
                if (dc_in_flg) {
                    p_dc_in_str = "DC";
                    if (usb_in_flg) dc_or_usb = "/";
                }
                if (usb_in_flg) {
                    switch (usb_infor) {
                        case USB_INFOR_unknown:
                            p_usb_in_str = "USB";
                            break;
                        case USB_INFOR_PD2_0:
                            p_usb_in_str = "PD";
                            break;
                        case USB_INFOR_BC1_2:
                            p_usb_in_str = "USB";
                            break;
                        case USB_INFOR_QC2_0:
                            p_usb_in_str = "QC";
                            break;
                        case USB_INFOR_QC3_0:
                            p_usb_in_str = "QC";
                            break;
                        case USB_INFOR_PD3_0:
                            p_usb_in_str = "PD";
                            break;
                        case USB_INFOR_CDP:
                            p_usb_in_str = "CDP";
                            break;
                        case USB_INFOR_SDP:
                            p_usb_in_str = "SDP";
                            break;

                        default:
                            break;
                    }
                }
                sprintf(str_buf, "%s%s%s", p_dc_in_str, dc_or_usb, p_usb_in_str);
                DDL_Printf("send input information:%s\n", str_buf);
                usr_uart_send_a_msg(INPUT_STATE_CMD, str_buf);
            } else {
            }
        } else {
            DDL_Printf("error:%d\n", ret);
        }
    }
}